package com.a16.yuejian.yuejian.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Question;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 55080 on 2017/10/17.
 */

public class QuestionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private SharedPreferences sp;
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public QuestionAdapter(Context mContext){
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setList(List mDataList){
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.question_cardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Question entity = (Question) mDataList.get(position);
        if (null == entity){
            return;
        }else {
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.qt_uname.setText(entity.getUsername()+"：");
            viewHolder.qt_mname.setText(sp.getString("coachname","")+"：");
            viewHolder.qt_qt.setText(entity.getQuestion());
            viewHolder.qt_answer.setText(entity.getAnswer());
            Calendar cal = Calendar.getInstance();
            Date t1 = new Date(System.currentTimeMillis());
            cal.setTime(t1);
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar get = Calendar.getInstance();
            try {
                Date t2 =format.parse(entity.getTime());
                get.setTime(t2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (cal.get(Calendar.YEAR)==(get.get(Calendar.YEAR))){
                int day = cal.get(Calendar.DAY_OF_YEAR)-get.get(Calendar.DAY_OF_YEAR);
                if (day==0){
                    viewHolder.qt_time.setText("今天");
                }else if (day == 1){
                    viewHolder.qt_time.setText("昨天");
                }else{
                    String a =String.valueOf(day);
                    viewHolder.qt_time.setText((get.get(Calendar.MONTH)+1)+"-"+get.get(Calendar.DAY_OF_MONTH));
                }
            }else if (cal.get(Calendar.YEAR)!=(get.get(Calendar.YEAR))){
                viewHolder.qt_time.setText(get.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+get.get(Calendar.DAY_OF_MONTH));
            }

        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView qt_uname,qt_mname,qt_qt,qt_answer,qt_time;
        LinearLayout qt_container;

        public ViewHolder(View itemView) {
            super(itemView);
            qt_uname = (TextView) itemView.findViewById(R.id.qt_uname);
            qt_mname = (TextView) itemView.findViewById(R.id.qt_mname);
            qt_qt = (TextView) itemView.findViewById(R.id.qt_qt);
            qt_answer = (TextView) itemView.findViewById(R.id.qt_answer);
            qt_time = (TextView)itemView.findViewById(R.id.qt_time);
            qt_container= (LinearLayout)itemView.findViewById(R.id.qt_container);
            sp = mContext.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        }
    }
}
